﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DelonixRegiaHMSys.Core.Interfaces;
using DelonixRegiaHMSys.Core.Models;
using DelonixRegiaHMSys.Infrastructure.Data;
using System.Data.Objects;

namespace DelonixRegiaHMSys.Infrastructure.Data
{
    public class EfStaffQuery : IStaffQuery
    {
        public IEnumerable<Staff> GetAll()
        {
            var context = new hmsDBEntities();
            return context.staffDetails.Select(f => new Staff()
            {
                StaffID = f.staffID,
                StaffLastName = f.staffLastName,
                StaffFirstName = f.staffFirstName,
                DateOfBirth = f.DOB,
                BankAccNum = f.bankAccNum,
                HomeAddress = f.homeAddress,
                PhoneNum = f.phoneNum,
                DutiesName = f.dutiesName
            });
        }

        public void AddStaff(Staff st)
        {
            var context = new hmsDBEntities();
            var newStaff = context.staffDetails.Create();
            newStaff.staffID = st.StaffID;
            newStaff.staffLastName = st.StaffLastName;
            newStaff.staffFirstName = st.StaffFirstName;
            newStaff.DOB = st.DateOfBirth;
            newStaff.bankAccNum = st.BankAccNum;
            newStaff.homeAddress = st.HomeAddress;
            newStaff.phoneNum = st.PhoneNum;
            newStaff.dutiesName = st.DutiesName;
            context.staffDetails.Add(newStaff);
            context.SaveChanges();
        }

        public void DeleteStaff(string staffID)
        {
            var context = new hmsDBEntities();
            var staffToDelete = context.staffDetails.FirstOrDefault(s => s.staffID == staffID);
            context.staffDetails.Remove(staffToDelete);
            context.SaveChanges();
        }

        public void UpdateStaff(Staff st)
        {
            var context = new hmsDBEntities();
            var staffToUpdate = context.staffDetails.FirstOrDefault(s => s.staffID == st.StaffID);
            staffToUpdate.staffID = st.StaffID;
            staffToUpdate.staffLastName = st.StaffLastName;
            staffToUpdate.staffFirstName = st.StaffFirstName;
            staffToUpdate.DOB = st.DateOfBirth;
            staffToUpdate.bankAccNum = st.BankAccNum;
            staffToUpdate.homeAddress = st.HomeAddress;
            staffToUpdate.phoneNum = st.PhoneNum;
            staffToUpdate.dutiesName = st.DutiesName;
            context.Entry(staffToUpdate).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public IEnumerable<Staff> RetrieveStaffbydutiesName(string dutiesName)
        {
            var context = new hmsDBEntities();
            var query = from s in context.staffDetails
                        where s.dutiesName == dutiesName
                        select new Staff()
                        {
                            StaffID = s.staffID
                        };

            return query;
        }
    }
}